﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Asol.Reporting.Support.Parser;
using System.Drawing;

namespace Asol.Reporting.Editor.Expression
{
    public class FormulaParserSetting
    {
        #region Settings pro parsování vzorce
        public static ParserSetting CreateParserSetting()
        {
            ParserSetting setting = new ParserSetting(SETTING_NAME, SEGMENT_TEXT);
            ParserSegmentSetting segment;

            // Společná data pro SQL_CODE a SQL_PARENTHESIS, které obě mohou obsahovat SQL kód:
            string[] blanks = new string[] { " ", "\t", "\r", "\n" };
            string[] delimiters = new string[] { ",", "+", "-", "*", "/", "<", "=", ">", "<=", ">=", "<>", "%", "!", "!=", "^" };

            // Text:
            segment = new ParserSegmentSetting(SEGMENT_TEXT);
            segment.Blanks = blanks.ToArray();
            segment.Delimiters = delimiters.ToArray();
            segment.EndWith = null;
            segment.InnerSegmentsNames = new string[] { SEGMENT_STRING, SEGMENT_PARENTHESIS };
            segment.Keywords = GetKeywordsSqlLanguage().ToArray();
            segment.KeywordsCaseSensitive = false;
            segment.AddRtfCodes(ParserSegmentValueType.Text,
                                new RtfItem[] { RtfItem.NewForeColor(Color.DarkGreen), RtfItem.NewFont("Courier New CE"), RtfItem.NewFontStyle(RtfFontStyle.Regular) },
                                null);
            segment.AddRtfCodes(ParserSegmentValueType.Delimiter,
                                new RtfItem[] { RtfItem.NewForeColor(Color.Magenta) },
                                new RtfItem[] { RtfItem.NewForeColor(Color.DarkGreen) });
            segment.AddRtfCodes("Keyword",
                                new RtfItem[] { RtfItem.NewForeColor(Color.MediumBlue) },
                                new RtfItem[] { RtfItem.NewForeColor(Color.DarkGreen) });
            setting.SegmentSettingAdd(segment);

            // Stringy:
            segment = new ParserSegmentSetting(SEGMENT_STRING);
            segment.BeginWith = "\"";
            segment.EndWith = new string[] { "\"" };
            segment.SpecialTexts = new ParserSegmentSpecialTexts[] { new ParserSegmentSpecialTexts("\"\"", "\"") };
            segment.AddRtfCodes(ParserSegmentValueType.Text,
                                new RtfItem[] { RtfItem.NewForeColor(Color.Red), RtfItem.NewFont("Courier New CE"), RtfItem.NewFontStyle(RtfFontStyle.BoldOnly) },
                                new RtfItem[] { RtfItem.NewForeColor(Color.Empty) });
            setting.SegmentSettingAdd(segment);

            // Kulaté závorky = funkce, výrazy:
            segment = new ParserSegmentSetting(SEGMENT_PARENTHESIS);
            segment.BeginWith = "(";
            segment.Blanks = blanks.ToArray();
            segment.Delimiters = delimiters.ToArray();
            segment.EndWith = new string[] { ")" };
            segment.InnerSegmentsNames = new string[] { SEGMENT_TEXT, SEGMENT_STRING, SEGMENT_PARENTHESIS };
            segment.Keywords = GetKeywordsSqlLanguage().ToArray();
            segment.KeywordsCaseSensitive = false;
            segment.AddRtfCodes(ParserSegmentValueType.Text,
                                new RtfItem[] { RtfItem.NewForeColor(Color.DarkSlateBlue), RtfItem.NewFont("Courier New CE"), RtfItem.NewFontStyle(RtfFontStyle.Regular) },
                                new RtfItem[] { RtfItem.NewForeColor(Color.Empty) });
            segment.AddRtfCodes(ParserSegmentValueType.Delimiter,
                                new RtfItem[] { RtfItem.NewForeColor(Color.Magenta) },
                                new RtfItem[] { RtfItem.NewForeColor(Color.DarkSlateBlue) });
            segment.AddRtfCodes("Keyword",
                                new RtfItem[] { RtfItem.NewForeColor(Color.MediumBlue) },
                                new RtfItem[] { RtfItem.NewForeColor(Color.DarkGreen) });
            setting.SegmentSettingAdd(segment);

            // Hotovo:
            return setting;
        }
        public const string SETTING_NAME = "Formula";
        public const string SEGMENT_TEXT = "Text";
        public const string SEGMENT_STRING = "String";
        public const string SEGMENT_PARENTHESIS = "Parenthesis";

        #endregion
        #region Keywords
        /// <summary>
        /// Vrátí pole klíčových slov aktuálního jazyka (získaná refexí kódu).
        /// </summary>
        /// <returns></returns>
        public static List<string> GetKeywordsSqlLanguage()
        {
            return Evaluator.FunctionKeyList;
        }
        #endregion
    }
}
